<template>
	<div class="login_background">
		<div class="login_background_front"></div>
		<div class="login_main">
			<div class="login-form">
				<a-card>
					<div class="login-header">
						<h2>三方登录</h2>
					</div>
					<a-spin tip="正在登录中...">
						<div class="h-[300px]">
							<a-skeleton />
						</div>
					</a-spin>
				</a-card>
			</div>
		</div>
	</div>
</template>

<script setup name="loginCallback">
	import { message } from 'ant-design-vue'
	import tool from '@/utils/tool'
	import router from '@/router'
	import thirdApi from '@/api/auth/thirdApi'
	import loginApi from '@/api/auth/loginApi'
	import userCenterApi from '@/api/sys/userCenterApi'
	import dictApi from '@/api/dev/dictApi'
	import { globalStore, iframeStore, keepAliveStore, viewTagsStore } from '@/store'
	import configData from '@/config'
	import configApi from '@/api/dev/configApi'
	import { afterLogin } from '@/views/auth/login/util'

	const store = globalStore()
	const sysBaseConfig = computed(() => {
		return store.sysBaseConfig
	})

	const kStore = keepAliveStore()
	const iStore = iframeStore()
	const vStore = viewTagsStore()

	const setSysBaseConfig = store.setSysBaseConfig
	const clearKeepLive = kStore.clearKeepLive
	const clearIframeList = iStore.clearIframeList
	const clearViewTags = vStore.clearViewTags

	onBeforeMount(() => {
		clearViewTags()
		clearKeepLive()
		clearIframeList()
	})

	onMounted(() => {
		// if (isWeChatMiniProgram()) {
		// 	message.info('检测到微信小程序环境')
		// } else {
		// 	message.error('非微信小程序环境，请使用微信小程序打开')
		// }
		const url = new URL(window.location.href)

		const params = {}
		let isHasCode = false
		let isHasUniCode = false

		url.searchParams.forEach((value, key) => {
			params[key] = value
			if (key === 'code') {
				isHasCode = true
			}
			if (key === 'unicode') {
				isHasUniCode = true
			}
		})

		// 必须要有code参数
		if (!isHasCode) {
			message.error('code参数不存在，请重新登录')
			// console.error('code参数不存在')
		} else {
			// message.info('code:', params['code'])
			// console.info('code:', params['code'])
		}
		// 必须要有code参数
		if (!isHasUniCode) {
			message.error('unicode参数不存在，请重新登录')
			// console.error('unicode参数不存在')
		} else {
			// message.info('unicode:', params['unicode'])
			// console.info('unicode:', params['unicode'])
		}
		storeCode(params['unicode'], params['code'])
	})

	const storeCode = (uniLoginCode, SSOCode) => {
		if (uniLoginCode && SSOCode) {
			loginApi
				.storeUniLoginCode({
					uniappLoginCode: uniLoginCode,
					SCNUSSOCode: SSOCode
				})
				.then((result) => {
					if (result) {
						// message.success('code存储成功')
					} else {
						message.error('code存储失败，请重新登录')
					}
				})
		}
	}

	const getUrlCode = () => {
		const url = new URL(window.location.href)
		// message.info('当前url:', url)

		const params = {}
		let isHasCode = false

		url.searchParams.forEach((value, key) => {
			params[key] = value
			if (key === 'code') {
				isHasCode = true
			}
		})

		// 必须要有code参数
		if (!isHasCode) {
			message.error('code参数不存在，请重新登录')
		} else {
			return params.code
		}
	}

	// 检查是否在微信小程序环境
	const isWeChatMiniProgram = () => {
		const userAgent = navigator.userAgent.toLowerCase()
		return /micromessenger/i.test(userAgent) && /miniprogram/i.test(userAgent)
	}

	// logo链接
	const handleLink = (e) => {
		if (!sysBaseConfig.value.SNOWY_SYS_COPYRIGHT_URL) {
			e?.stopPropagation()
			e?.preventDefault()
		}
	}
</script>

<style lang="less" scoped>
	@import 'login';
</style>
